package com.wgxmkalftp.usercenter.service;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.wgxmkalftp.pojo.User;
import com.wgxmkalftp.user.service.CodeService;
import com.wgxmkalftp.user.service.UserService;
import com.wgxmkalftp.usercenter.dao.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.Duration;
import java.util.Date;

/**
 * @author wgx
 * @description
 * @create 2021/3/9/11:26/
 * @origin packageName:com.wgxmkalftp.usercenter.service    projectName:PersonalityWallet
 * @since
 */
@Service(version = "1.0.0")
public class UserServiceImpl implements UserService {

    /*@Reference(version = "1.0.0")
    private CodeService codeService;*/

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean register(User user, String checkCode) {
       try {
           if (user==null){
               return null;
           }
          /* //校验验证码
           Boolean bool = codeService.verifyCode(user.getPhone(), checkCode);*/
               String redisKey="USER_CODE_"+user.getPhone();
               if (!redisTemplate.hasKey(redisKey)){
                   return false;
               }
               String checkCodeVal = redisTemplate.opsForValue().get(redisKey);
               if (StringUtils.equals(checkCode,checkCodeVal)){
                   user.setCreate_time(new Date());
                   userMapper.insert(user);
               }
           Boolean bool = redisTemplate.delete(redisKey);
           return bool;

           /*if (bool){
               user.setCreate_time(new Date());
               userMapper.insert(user);
           }
           return bool;*/
       }catch (Exception e){
           e.printStackTrace();
       }
       return false;
    }

    @Override
    public String sendCode(String phone) {
        try {
            String redisKey="USER_CODE_"+phone;
            if (redisTemplate.hasKey(redisKey)){
                return "验证码还未过期";
            }
            String code="123456";
            redisTemplate.opsForValue().set(redisKey,code, Duration.ofMinutes(5));
            return code;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
